Shared Contexts
3.4.6A shared context is a named object (holding a map
of key/value pairs) that stores cross application data.
The context object can hold any cross-application data. Any application can update a context or subscribe for context updates
and react to them by using the name of the context.
The Shared Contexts API offers a simple and effective solution for sharing data between your applications. Imagine you have an application showing a list of clients and an application showing client portfolios. What you need, is your "Portfolio" app to show the portfolio of a specific client that the user has selected from the "Clients" app. You can easily achieve this in a few simple steps by using the Shared Contexts API:
- instruct the "Clients" app to publish updates to a context object holding the
id
of the currently selected client; - instruct the "Portfolio" app to subscribe for updates of that same context object and specify how the "Portfolio" app should handle the received data in order to update its current state;
The Shared Contexts API is accessible through the io.contexts
object.
APIobject
Methods
allmethod
Signature
() => string[]
Description
Returns all existing context names. Using the context name you can subscribe for context changes, updates or set context values.
destroymethod
Signature
(name: string) => Promise<any>
Description
Destroys a context and all the data associated with it
Parameters
Name | Type | Required | Description |
---|---|---|---|
name | string | Name of the context to be removed |
getmethod
Signature
(name: string) => Promise<any>
Description
Return the context data immediately or asynchronously as soon as any data becomes available.
Parameters
Name | Type | Required | Description |
---|---|---|---|
name | string | Name of the context from which you want to get data. |
setmethod
Signature
(name: string, data: any) => Promise<void>
Description
Replaces a context. All properties of the specified context object will be removed and replaced with the ones supplied in the data
parameter.
Parameters
Name | Type | Required | Description |
---|---|---|---|
name | string | Name of the context to be replaced. |
|
data | any | The object that will be applied to the context. |
setPathmethod
Signature
(name: string, path: string, data: any) => Promise<void>
Description
Sets a path in the context to some value. Use this to update values that are not on top level in the context.
Parameters
Name | Type | Required | Description |
---|---|---|---|
name | string | Name of the context to be updated |
|
path | string | Path to be updated. Path should be in the format "prop1.prop2" |
|
data | any | The object that will be applied to the path |
setPathsmethod
Signature
(name: string, paths: PathValue[]) => Promise<void>
Description
Sets multiple paths in the context to some values in a single command.
Parameters
Name | Type | Required | Description |
---|---|---|---|
name | string | Name of the context to be updated |
|
paths | PathValue[] | Array of paths and their values to be updated. Path should be in the format "prop1.prop2" |
subscribemethod
Signature
(name: string, callback: (data: any, delta: any, removed: string[], unsubscribe: () => void, extraData?: any) => void) => Promise<() => void>
Description
Subscribes for context events. Returns an unsubscribe function which you can use to stop receiving context updates.
Parameters
Name | Type | Required | Description |
---|---|---|---|
name | string | Name of the context to which you want to subscribe. |
|
callback | (data: any, delta: any, removed: string[], unsubscribe: () => void, extraData?: any) => void | Function that will handle the updates. |
updatemethod
Signature
(name: string, data: any) => Promise<void>
Description
Updates a context with the supplied object. This method updates only the specified context properties. Any other existing context properties will remain intact.
If the context does not exist, the update()
method will create it.
Parameters
Name | Type | Required | Description |
---|---|---|---|
name | string | Name of the context to be updated. |
|
data | any | The object that will be applied to the context. |
Example
io.contexts.update("app-styling", {
backgroundColor: "red",
alternativeColor: "green"
});
PathValueobject
Properties
Property | Type | Default | Required | Description |
---|---|---|---|---|
path | string | |||
value | any |